<template>
  <el-form :inline="true" :model="formInline" class="user-search">
      <el-select v-model="find_value" placeholder="请选择">
        <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
        >
        </el-option>
      </el-select>
      <el-form-item label="搜索：">
        <el-input size="small" v-model="find_p" :placeholder='"请输入"+find_value' type="email"></el-input>
      </el-form-item>
      
      
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
</template>

<script>
import userutil from '../utils/userutil'
export default {
data(){
    return{
        formInline: {
            username:'',
            mail:'',
            phonenumber:''
        },
        options:[
            {
                value:'username',
                label:'用户名'
            },{
                value:'mail',
                label:'邮箱'
            },{
                value:'phonenumber',
                label:'手机号'
            }
        ],
        
        find_value:'',
        find_p:'',
    }
},
methods:{
    search(){
        var flag=false
        const mailrule=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
        const phonenumberrule=/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
        // if(this.find_value=="username") this.formInline.username=this.find_p
        // else if(this.find_value=="mail")this.formInline.mail=this.find_p
        // else if(this.find_value=="phonenumber")this.formInline.phonenumber=this.find_p
       if(this.find_value=="mail"){
           if(!mailrule.test(this.find_p)){
               this.$message.error("邮箱格式不正确，请重新输入！")
           }else{flag=true}
       }else if(this.find_value=="phonenumber"){
           if(!phonenumberrule.test(this.find_p)){
               this.$message.error("手机号格式不正确，请重新输入！")
           }else{flag=true}
       }
       if(flag){ 
        var param=this.find_value
        this.$axios.get('/api/userList/PersonSearch',{
            params:{
                param_name:param,
                param:this.find_p
        }
        }).then(res=>{
            // var temp=JSON.stringify(res.data)
            // localStorage.setItem('findP_data',temp)
            // console.log("已经转为string格式传入localstorage")

            console.log(this.find_p)
            console.log(res)
            //传递值
            userutil.$emit('userdata',res.data)
            

            if(res.data.username=="无"){
                this.$message.error("该用户不存在，请检查您的输入信息")
                console.log("返回结果为空")
            }
            
        })
       }
       
    },
}
}
</script>

<style>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>